package com.is.control;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.is.dbconn.DBConnect;
import com.is.model.TeacherAssess;


/*用于管理教师业绩考核（课时）
 */
public class TeacherAssessControl {
	
	
	public static boolean insertTeacherAssess(TeacherAssess ta){
	DBConnect dbConnect = new DBConnect();
	String sql = "insert into teacher_assess (uid,time,classAmount,courseClassId,absentAmount,extraAmount,leaveAmount) values(?,?,?,?,?,?,?)";
	dbConnect.prepareStatement(sql);
	try{
		setValue(ta, dbConnect);
		if(dbConnect.executeUpdate()){
			return true;
		}
	}catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		System.out.println("insertTeacherAssess ERROR");
	}finally{
		dbConnect.close();
	}
	return false;
	}
	
	
	/**
	 * 批量插入教师课时
	 * @param tas
	 * @return
	 */
	public static boolean insertTeacherAssess(List<TeacherAssess> tas){
		DBConnect dbConnect = new DBConnect();
		PreparedStatement pstmt = null;
		String sql="insert into teacher_assess (uid,time,classAmount,courseClassId,absentAmount,extraAmount,leaveAmount) values(?,?,?,?,?,?,?)";
		dbConnect.setAutoCommit(false);
		dbConnect.prepareStatement(sql);
		pstmt = dbConnect.getPreparedStatement();
		try {
			for(TeacherAssess ta:tas){
				int index=1;
				pstmt.setInt(index++, ta.getUid());
				pstmt.setString(index++, ta.getTime());
				pstmt.setString(index++,ta.getClassAmount());
				pstmt.setInt(index++, ta.getCourseClassId());
				pstmt.setString(index++, ta.getAbsentAmount());
				pstmt.setString(index++, ta.getExtraAmount());
				pstmt.setString(index++, ta.getLeaveAmount());
				CourseClassControl.updatetRealToalClassById(ta.getCourseClassId(),ta.getClassAmount());
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			dbConnect.commit();
			dbConnect.setAutoCommit(true);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("insertTeacherAssess(List<TeacherAssess>) ERROR");
		}finally{
			dbConnect.close();
		}
		return false;
	}
	
	/**
	 * 单独插入教师课时
	 * @param tas
	 * @return
	 */
	public static boolean insertTeacherSingleAssess(TeacherAssess ta){
		DBConnect dbConnect = new DBConnect();
		boolean flag = false;
		String sql="insert into teacher_assess (uid,time,classAmount,courseClassId,absentAmount,extraAmount,leaveAmount) values(?,?,?,?,?,?,?)";
		dbConnect.prepareStatement(sql);
		try {
			int index=1;
			dbConnect.setInt(index++, ta.getUid());
			dbConnect.setString(index++, ta.getTime());
			dbConnect.setString(index++,ta.getClassAmount());
			dbConnect.setInt(index++, ta.getCourseClassId());
			dbConnect.setString(index++, ta.getAbsentAmount());
			dbConnect.setString(index++, ta.getExtraAmount());
			dbConnect.setString(index++, ta.getLeaveAmount());
			flag = dbConnect.executeUpdate()&&CourseClassControl.updatetRealToalClassById(ta.getCourseClassId(), ta.getClassAmount());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("insertTeacherSingleAssess ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}
	
	/**统计某个时间段内单个老师的课时和
	 * 
	 * @param uid
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static Map<String,String> countAmountPeriod(int uid,int courseClassId,String time1,String time2){
		DBConnect dbConnect = new DBConnect();
		Map<String,String>  map = new HashMap<String,String> ();
		String classAmount = "0.0";
		String absentAmount="0.0";
		String extraAmount="0.0";
		String leaveAmount="0.0";
		String sql = "select sum(classAmount) as c,sum(absentAmount) as a ,sum(extraAmount) as e ,sum(leaveAmount)as l from teacher_assess where uid = '"+uid+"' and courseClassId='"+courseClassId+"' and (time between '"+time1+"' and '"+time2+"')";
		dbConnect.prepareStatement(sql);
		//System.out.println(sql);
		ResultSet rSet = null;
		try {
			rSet = dbConnect.executeQuery();
			if(rSet.next()){
			    classAmount= rSet.getString("c");
			    absentAmount = rSet.getString("a");
			    extraAmount = rSet.getString("e");
			    leaveAmount = rSet.getString("l");
			    map.put("classAmount", classAmount==null?"0.0":classAmount);
			    map.put("absentAmount", absentAmount==null?"0.0":absentAmount);
			    map.put("extraAmount", extraAmount==null?"0.0":extraAmount);
			    map.put("leaveAmount", leaveAmount==null?"0.0":leaveAmount);
//			    System.out.println(total);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("countClassAmountPeriod ERROE");
		}finally{
			dbConnect.close();
		}
		return map;
	}
	
	/**
	 * 获取上一次提交教师考核时间
	 * @return
	 */
	public static String getLastInsertTime(){
		DBConnect dbConnect = new DBConnect();
		String sql = "select max(time) from teacher_assess";
		ResultSet rs = null;
		String time="";
		dbConnect.prepareStatement(sql);
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()&&rs.getString(1)!=null){
				time= rs.getString(1); 
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			dbConnect.close();
		}
		return time;
	}
	/**统计某个时间段内老师的课时
	 * 
	 * @param uid
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static List<TeacherAssess> getTeacherAssessPeriod(int uid,String time1,String time2){
		DBConnect dbConnect = new DBConnect();
		List<TeacherAssess> tas = new ArrayList<TeacherAssess>();
		String sql = "select * from teacher_assess where uid = '"+uid+"' and (time between '"+time1+"' and '"+time2+"')";
		dbConnect.prepareStatement(sql);
		ResultSet rSet = null;
		try {
			rSet = dbConnect.executeQuery();
			while(rSet.next()){
				//return rSet.getString(1);
				TeacherAssess ta = new TeacherAssess();
				getRs(ta, rSet);
				tas.add(ta);
			}
			return tas;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("getTeacherAssessPeriod ERROR");
		}finally{
			dbConnect.close();
		}
		return null;
		
	}
	public static void getRs(TeacherAssess ta,ResultSet rs) throws SQLException{
		ta.setClassAmount(rs.getString("classAmount"));
		ta.setCourseClassId(rs.getInt("courseClassId"));
		ta.setTime(rs.getString("time"));
		ta.setUid(rs.getInt("uid"));
	    ta.setId(rs.getInt("id"));
	    ta.setAbsentAmount(rs.getString("absenrAmount"));
	    ta.setClassAmount(rs.getString("classAmount"));
	    ta.setLeaveAmount(rs.getString("leaveAmount"));
	}
	public static void setValue(TeacherAssess ta,DBConnect dbc) throws SQLException{
		int index=1;
		dbc.setInt(index++, ta.getUid());
		dbc.setString(index++, ta.getTime());
		dbc.setString(index++, ta.getTime());
		dbc.setString(index++, ta.getClassAmount());
		dbc.setInt(index++, ta.getCourseClassId());
		dbc.setString(index++, ta.getAbsentAmount());
		dbc.setString(index++, ta.getExtraAmount());
		dbc.setString(index++, ta.getLeaveAmount());
	}

}
